# coding:utf-8
import os
import time
import random
from multiprocessing import Pool

    
print("-----------------------进程池----------------------")
def run_task2(name):
    print('Run task %s'% name)
    s_start = time.time()
    time.sleep(random.random())
    s_end = time.time()
    print('子进程：%s,父进程：%s,Task %s run %.2f sec' % (os.getpid(),os.getppid(),name,s_end - s_start))
if __name__ == '__main__':
    print('Parent Process %s is running...' % os.getpid())
    p = Pool() # 进程池的大小为1，即同时只能运行一个进程,不指定默认是CPU的核数
    for i in range(5):
        p.apply_async(run_task2,args=(i,))
    print('all subProcess will running...')
    p.close()
    p.join()
    print('all subProcess running ok')


    print("-----------------------外部子进程----------------------")
    import subprocess

    r = subprocess.call(['ping','www.python.org'])
    print(r)

    # subprocess 模块可以让我们非常方便地启动一个子进程，然后控制其输入和输出。
